#region Copyright (c) 2006-2012 Widgetsphere LLC, All Rights Reserved
//--------------------------------------------------------------------- *
//                          Widgetsphere  LLC                           *
//             Copyright (c) 2006-2012 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC   *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT.    *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE,      *
//INC.  THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO       *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Widgetsphere.Generator.Common.GeneratorFramework;
using Widgetsphere.Generator.Common.Util;

namespace Widgetsphere.Generator.Models
{
	//[Editor(typeof(Widgetsphere.Generator.Design.Editors.FunctionColumnCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
	public class FunctionColumnCollection : BaseModelCollection, IEnumerable<FunctionColumn>
	{
		#region Member Variables

		protected Hashtable _internalList;
		protected int _lastId = 1;

		#endregion

		#region Constructor

		public FunctionColumnCollection(IWidgetsphereModelObject root)
			: base(root)
		{
			_internalList = new Hashtable();
		}

		#endregion

		#region Property Implementations

		#endregion

		#region Methods

		public FunctionColumn[] GetById(int id)
		{
			var retval = new ArrayList();
			foreach(FunctionColumn element in this)
			{
				if(element.Id == id)
					retval.Add(element);
			}
			return (FunctionColumn[])retval.ToArray(typeof(FunctionColumn));
		}

		#endregion

		#region IXMLable Members

		public override void XmlAppend(XmlNode node)
		{
			var oDoc = node.OwnerDocument;

			XmlHelper.AddAttribute(node, "key", this.Key);

			var l = new List<FunctionColumn>();
			foreach (FunctionColumn c in _internalList.Values) l.Add(c);
			foreach (var FunctionColumn in l.OrderBy(x => x.Name))
			{
				var functionColumnNode = oDoc.CreateElement("functioncolumn");
				FunctionColumn.XmlAppend(functionColumnNode);
				node.AppendChild(functionColumnNode);
			}

		}

		public override void XmlLoad(XmlNode node)
		{
			try
			{
				_key = XmlHelper.GetAttributeValue(node, "key", string.Empty);
				var functionColumnNodes = node.SelectNodes("functioncolumn");
				foreach(XmlNode FunctionColumnNode in functionColumnNodes)
				{
					var newFunctionColumn = new FunctionColumn(this.Root);
					newFunctionColumn.XmlLoad(FunctionColumnNode);
					_internalList.Add(newFunctionColumn.Id, newFunctionColumn);
					if(_lastId < newFunctionColumn.Id)
						_lastId = newFunctionColumn.Id;
				}

				this.Dirty = false;
			}
			catch(Exception ex)
			{
				throw;
			}

		}

		#endregion

		#region IEnumerable Members
		public override IEnumerator GetEnumerator()
		{
			var al = new List<FunctionColumn>();
			foreach(int key in _internalList.Keys)
				al.Add((FunctionColumn)_internalList[key]);
			return al.GetEnumerator();
		}
		#endregion

		#region IDictionary Members

		public bool IsReadOnly
		{
			get { return _internalList.IsReadOnly; }
		}

		public FunctionColumn this[int FunctionColumnId]
		{
			get { return (FunctionColumn)_internalList[FunctionColumnId]; }
			set { _internalList[FunctionColumnId] = value; }
		}

		public void Remove(int FunctionColumnId)
		{
			this.Root.Dirty = true;
			_internalList.Remove(FunctionColumnId);
		}

		public void Remove(FunctionColumn customStoredProcedureColumn)
		{      
			this.Remove(customStoredProcedureColumn.Id);
		}

		public bool Contains(int customStoredProcedureColumnId)
		{
			return _internalList.Contains(customStoredProcedureColumnId);
		}

		public override void Clear()
		{
			_internalList.Clear();
		}

		private void Add(FunctionColumn value)
		{
			_internalList.Add(value.Id, value);
		}

		public FunctionColumn Add(string name)
		{
			var newItem = new FunctionColumn(this.Root);
			newItem.Name = name;
			_lastId++;
			newItem.SetId(_lastId);
			this.Add(newItem);
			return newItem;
		}

		public override void AddRange(ICollection list)
		{
			foreach(FunctionColumn element in list)
			{
				_lastId++;
				element.SetId(_lastId);
				_internalList.Add(_lastId, element);
			}
		}

		public FunctionColumn Add()
		{
			return this.Add(this.GetUniqueName());
		}

		public bool IsFixedSize
		{
			get { return _internalList.IsFixedSize; }
		}

		public bool Contains(string name)
		{
			foreach(FunctionColumn customStoredProcedureColumn in this)
			{
				if(string.Compare(customStoredProcedureColumn.Name, name, true) == 0)
				{
					return true;
				}
			}
			return false;
		}

		private string GetUniqueName()
		{
			//const string baseName = "Column";
			//int ii = 1;
			//string newName = baseName + ii.ToString();
			//while(this.Contains(newName))
			//{
			//  ii++;
			//  newName = baseName + ii.ToString();
			//}
			//return newName;
			return "[NEW COLUMN]";
		}

		#endregion

		#region ICollection Members

		public override bool IsSynchronized
		{
			get
			{
				return _internalList.IsSynchronized;
			}
		}

		public override int Count
		{
			get
			{
				return _internalList.Count;
			}
		}

		public override void CopyTo(Array array, int index)
		{
			_internalList.CopyTo(array, index);
		}

		public override object SyncRoot
		{
			get
			{
				return _internalList.SyncRoot;
			}
		}

		#endregion

		#region IEnumerable<FunctionColumn> Members

		IEnumerator<FunctionColumn> IEnumerable<FunctionColumn>.GetEnumerator()
		{
			var al = new List<FunctionColumn>();
			foreach (int key in _internalList.Keys)
				al.Add((FunctionColumn)_internalList[key]);
			return al.GetEnumerator();
		}

		#endregion

	}
}
